﻿using Google.Protobuf.WellKnownTypes;
using MyWeb.ScheduledTasks.Protos;
using Nacos.V2.Utils;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using static Quartz.Logging.OperationName;

namespace MyWeb.ScheduledTasks.Models
{
    /// <summary>
    /// 任务表
    /// </summary>
    [Description("任务表")]
    [Table("JobsModel")]
    public class Jobs
    {
        /// <summary>
        /// 主键Id
        /// </summary>
        [Description("主键Id")]
        public long Id { get; set; }
        /// <summary>
        /// 执行开始时间
        /// </summary>
        [Description("执行开始时间")]
        public DateTime? BeginDate { get; set; }
        /// <summary>
        /// 执行结束时间
        /// </summary>
        [Description("结束时间")]
        public DateTime? EndDate { get; set; }
        /// <summary>
        /// 执行响应消息
        /// </summary>
        [Description("执行响应消息")]
        public string Msg { get; set; } = string.Empty;

        /// <summary>
        /// 任务名
        /// </summary>
        [Description("任务名")]
        public string TaskName { get; set; } = string.Empty;
        /// <summary>
        /// 分组名
        /// </summary>
        [Description("分组名")]
        public string GroupName { get; set; } = string.Empty;
        /// <summary>
        /// 间隔时间
        /// </summary>
        [Description("间隔时间")]
        public string Interval { get; set; } = string.Empty;
        /// <summary>
        /// 任务描述
        /// </summary>
        [Description("任务描述")]
        public string Describe { get; set; } = string.Empty;
        /// <summary>
        /// 最近一次运行时间
        /// </summary>
        [Description("最近一次运行时间")]
        public DateTime? LastRunTime { get; set; }
        /// <summary>
        /// 运行状态
        /// </summary>
        [Description("运行状态")]
        public int Status { get; set; }
        /// <summary>
        /// 任务类型(1.DLL类型,2.API类型)
        /// </summary>
        [Description("任务类型(1.DLL类型,2.API类型)")]
        public int TaskType { get; set; }

        #region Api类型专用参数
        /// <summary>
        /// 调用的API地址
        /// </summary>
        [Description("调用的API地址")]
        public string ApiUrl { get; set; } = string.Empty;
        /// <summary>
        /// API访问类型(API类型)
        /// </summary>
        [Description("API访问类型(API类型)")]
        public string ApiRequestType { get; set; } = string.Empty;
        /// <summary>
        /// Api头部参数JSON(API类型)
        /// </summary>
        /// 
        [Description("Api头部参数JSON(API类型)")]
        public string ApiHeader { get; set; } = string.Empty;
        /// <summary>
        /// ApiBody参数JSON(API类型)
        /// </summary>
        /// 
        [Description("ApiBody参数JSON(API类型)")]
        public string ApiBody { get; set; } = string.Empty;
        /// <summary>
        /// 请求参数【键值JSON】
        /// </summary>
        /// 
        [Description("请求参数")]
        public string Parameter { get; set; } = string.Empty;
        #endregion

        #region DLL类型专用参数
        /// <summary>
        /// DLL名称
        /// </summary>
        [Description("DLL名称")]
        public string? DllName { get; set; }
        /// <summary>
        /// DLL类型名
        /// </summary>
        [Description("DLL类型名")]
        public string DllClassName { get; set; } = string.Empty;

        /// <summary>
        /// Dll方法名
        /// </summary>
        [Description("Dll方法名")]
        public string DllActionName { get; set; } = string.Empty;
        #endregion

        //[SugarColumn(IsIgnore = true)]
        public ResponseJob.Types.JobsModel GetResponseJob()
        {
            return new ResponseJob.Types.JobsModel()
            {
                Id = this.Id,
                BeginDate = Timestamp.FromDateTime(DateTime.SpecifyKind((DateTime)this.BeginDate, DateTimeKind.Utc)),
                EndDate = Timestamp.FromDateTime(DateTime.SpecifyKind((DateTime)this.EndDate, DateTimeKind.Utc)),
                Msg = this.Msg,
                TaskName = this.TaskName,
                GroupName = this.GroupName,
                Interval = this.Interval,
                Describe = this.Describe.IsNullOrWhiteSpace() ? "" : this.Describe,
                LastRunTime = Timestamp.FromDateTime(DateTime.SpecifyKind((DateTime)this.LastRunTime, DateTimeKind.Utc)),
                Status = this.Status,
                TaskType = this.TaskType,
                ApiUrl = this.ApiUrl.IsNullOrWhiteSpace() ? "" : this.ApiUrl,
                ApiRequestType = this.ApiRequestType.IsNullOrWhiteSpace() ? "" : this.ApiRequestType,
                ApiHeader = this.ApiHeader.IsNullOrWhiteSpace() ? "" : this.ApiHeader,
                ApiBody = this.ApiBody.IsNullOrWhiteSpace() ? "" : this.ApiBody,
                Parameter = this.Parameter.IsNullOrWhiteSpace() ? "" : this.Parameter,
                DllName = this.DllName.IsNullOrWhiteSpace() ? "" : this.DllName,
                DllClassName = this.DllClassName.IsNullOrWhiteSpace() ? "" : this.DllClassName,
                DllActionName = this.DllActionName.IsNullOrWhiteSpace() ? "" : this.DllActionName,
            };
        }
    }


    public enum JobState
    {
        新增 = 1,
        删除 = 2,
        修改 = 3,
        暂停 = 4,
        停止 = 5,
        开启 = 6,
        立即执行 = 7
    }
}
